package com.p2p.front.dal.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.p2p.base.dao.BaseMapper;
import com.p2p.base.page.PageVo;
import com.p2p.front.dal.model.Tender;

/**
 * @author ruiqi
 *
 */
public interface TenderDao extends BaseMapper {

	void create(Tender tender);
	
	List<Tender> findByLoanId(Long lid);
	
	List<Tender> findSuccesedByLoanId(Long lid);
	
	
	List<Tender> findByUserId(Long userId);
	
	List<Tender> findByLoanIdAndStatus(@Param("lid")Long lid,@Param("status")int status);
	
	List<Tender> findByLoanIdAndStatusList(@Param("lid")Long lid, @Param("statusList")List<Integer> statusList);
	
	Tender getByTid(Long tid);
	
	Tender getByTidAndUid(@Param("tid")Long tid,@Param("uid")Long uid);
	
	@Update("update p2p_tender set status=#{status},uptime=NOW() where tid=#{tid} ")
	int updateTenderStatus(@Param("tid")Long tid,@Param("status")Integer status);
	
	@Update("update p2p_tender set status=#{status},loan_time=#{loanTime},uptime=NOW() where tid=#{tid}")
	int updateTenderStatusForLoanFull(@Param("tid")Long tid,@Param("status")Integer status,@Param("loanTime")Date loanTime);
	
	@Update("update p2p_tender set tstatus=#{tstatus},uptime=NOW() where tid=#{tid}")
	int updateTenderTstatus(@Param("tid")Long tid,@Param("tstatus")Integer tstatus);
	
	@Select("select * from p2p_tender order by lid desc")
	List<Map<String,Object>> userLoanList(PageVo form);

	@Select("select * from p2p_tender where lid = #{lid}")
	List<Map<String, Object>> findAll(Long lid);

	@Select("select * from p2p_tender where tid = #{tid}")
	Map<String, Object> get(Long tid);

	List<Map<String, Object>> userWaitList(@Param("uid") String uid,
			@Param("list") List<Map<String, Object>> waitLoanList);

	@Select("select count(*) from p2p_tender where uid=#{uid} and status=#{status}")
	Integer countByStatus(@Param("uid") Long uid,
			@Param("status") Integer status);

	@Select("select count(pt.lid) from p2p_tender pt where pt.uid = #{userId} and pt.status >= 11")
	int getAllNum(String userId);

	@Select("select sum(amount) from p2p_tender where uid= #{userId} and status >=11")
	Long sumTatalAmountByUid(String userId);

	@Select("select count(tid) from p2p_tender where lid = #{lid}")
	int getCountByLid(long lid);
}
